﻿using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Web.Http;
using AngularFirst.Models;

namespace AngularFirst.Controllers.API
{
    public class StudentController : ApiController
    {
        private readonly SchoolContext _schoolContext = new SchoolContext();

        [ActionName("GetStudents")]
        public IEnumerable<Student> GetStudents()
        {
            //Thread.Sleep(4000);
            var data = _schoolContext.Students.AsEnumerable();
            return data;
        }

        // GET api/<controller>/5
        [ActionName("GetStudent")]
        public Student GetStudent(int id)
        {
            //Thread.Sleep(4000);
            Student student = _schoolContext.Students.Find(id);
            if (student == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            return student;
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {

        }

        public HttpResponseMessage Add(Student student)
        {
            _schoolContext.Entry(student).State = EntityState.Added;
            try
            {
                _schoolContext.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        public HttpResponseMessage Update(Student student)
        {
            _schoolContext.Entry(student).State = EntityState.Modified;
            try
            {
                _schoolContext.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        public HttpResponseMessage Remove(int id)
        {
            try
            {
                Student student = _schoolContext.Students.Find(id);
                _schoolContext.Students.Remove(student);
                _schoolContext.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }

    }
}



//// GET api/<controller>
//        public IEnumerable<string> Get()
//        {
//            return new string[] { "value1", "value2" };
//        }

//        // GET api/<controller>/5
//        public string Get(int id)
//        {
//            return "value";
//        }

//        // POST api/<controller>
//        public void Post([FromBody]string value)
//        {
//        }

//        // PUT api/<controller>/5
//        public void Put(int id, [FromBody]string value)
//        {
//        }

//        // DELETE api/<controller>/5
//        public void Delete(int id)
//        {
//        }